#include "logging.h"
#include <iostream>
#include <chrono>



void Logging::log(const LogLevel &level, const std::string &message) const {
    std::cout << log_level_normal_string(level) << message << std::endl;
}

std::string Logging::get_time() const {
    std::stringstream stream;
    auto currentTime = std::chrono::system_clock::now();
    auto stampTime = std::chrono::system_clock::to_time_t(currentTime);
    tm newTime{};
#ifdef __WIN32
    localtime_s(&newTime, &stampTime);
#else
    localtime_r(&stampTime, &newTime);
#endif


    stream << std::put_time(&newTime, " %Y/%m/%d %H:%M:%S");

    return stream.str();
}


std::string Logging::log_level_colorful_string(const LogLevel &level) const {
    switch (level) {
        case LogLevel::DEBUG: return "\033[36m[DEBUG]: ";
        case LogLevel::INFO: return "\033[32m[INFO]: ";
        case LogLevel::WARNING: return "\033[33m[WARNING]: ";
        case LogLevel::ERROR: return "\033[31m[ERROR]: ";
        case LogLevel::CRITICAL: return "\033[43;30m[CRITICAL]: ";
        case LogLevel::FATAL: return "\033[41m[FATAL]: ";
        default: return "\033[32m[DEBUG]: ";
    }
}

std::string Logging::log_level_normal_string(const LogLevel &level) const {
    const auto time = get_time();
    switch (level) {
        case LogLevel::DEBUG: return time + " [DEBUG]: ";
        case LogLevel::INFO: return time + " [INFO]: ";
        case LogLevel::WARNING: return time + " [WARNING]: ";
        case LogLevel::ERROR: return time + " [ERROR]: ";
        case LogLevel::CRITICAL: return time + " [CRITICAL]: ";
        case LogLevel::FATAL: return time + " [FATAL]: ";
        default: return time + " [DEBUG]: ";
    }
}
